<template>
  <div>
    <div class="margin-bottom">
      <el-input
        clearable
        placeholder="输入企业名称搜索"
        size="mini"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search">搜索</el-button>
      <div style="float:right">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
      </div>
    </div>
    <el-table v-loading="loading" :data="companies" border style="width: 100%">
      <el-table-column label="企业Logo">
        <template slot-scope="scope">
          <img style="width:100px" :src="scope.row.logoUrl">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="企业全称"/>
      <el-table-column prop="shortName" label="企业简称"/>
      <el-table-column label="官方网站">
        <template slot-scope="scope">
          <el-link type="primary" :href="scope.row.webUrl" target="_blank">{{ scope.row.webUrl }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"/>
          <el-popover :ref="scope.row.uuid" placement="top" width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.uuid].doClose()">取消</el-button>
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="remove(scope.row)"
              >确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini" icon="el-icon-delete"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="margin-top"
      background
      layout="prev, pager, next"
      :page-count="pageConfig.total"
      @current-change="currentChange"
    />
    <el-dialog :close-on-click-modal="false" :title="!isEdit?'新建企业':'编辑企业'" :visible.sync="isShow" width="600px">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="企业全称" prop="name">
          <el-input
            v-model="form.name"
            auto-complete="off"
            maxlength="40"
            class="el-input-width"
          />
        </el-form-item>
        <el-form-item label="企业简称" prop="shortName">
          <el-input
            v-model="form.shortName"
            auto-complete="off"
            maxlength="20"
            class="el-input-width"
          />
        </el-form-item>
        <el-form-item label="企业logo" prop="logo">
          <span class="sign">(建议大小 100*100)</span>
          <el-upload
            class="avatar-uploader"
            :action="upload_url"
            accept="image/jpeg,image/png"
            :headers="Authorization"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :before-upload="beforeUpload"
          >
            <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="官网链接" prop="webUrl">
          <el-input
            v-model="form.webUrl"
            auto-complete="off"
            class="el-input-width"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button :loading="saveLoading" type="primary" @click="confirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { upload_url } from '@/api/file';
import { getToken } from '@/utils/auth';
import {
  company_add,
  company_update,
  company_remove,
  company_page
} from '@/api/system';
export default {
  data() {
    return {
      upload_url: upload_url,
      companies: [],
      loading: false,
      isShow: false,
      isEdit: true,
      delLoading: false,
      saveLoading: false,
      Authorization: {
        Authorization: getToken()
      },
      pageConfig: {
        page: 0,
        pageSize: 10,
        total: 0
      },
      form: {
        name: '',
        shortName: '',
        logoUrl: '',
        contract: 0,
        webUrl: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入企业全称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        shortName: [
          { required: true, message: '请输入企业简称', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        webUrl: [
          { max: 40, message: '最大长度为 40 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.getCompanies();
  },
  methods: {
    async getCompanies() {
      this.loading = true;
      const res = await company_page({
        page: this.pageConfig.page,
        pageSize: this.pageConfig.pageSize
      });
      this.companies = res.data.pageData;
      this.pageConfig.total = res.data.total;
      this.loading = false;
    },
    currentChange(value) {
      this.pageConfig.page = value - 1;
      this.getCompanies();
    },
    add() {
      this.isEdit = false;
      this.isShow = true;
      this.form = {};
    },
    edit(company) {
      this.isEdit = true;
      this.isShow = true;
      this.form = JSON.parse(JSON.stringify(company));
    },
    async remove(company) {
      this.$confirm('是否删除该权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await company_remove(company.uuid);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getCompanies();
      });
    },
    uploadSuccess(res, file) {
      this.$message({
        type: 'success',
        message: '上传成功!'
      });
      this.$set(this.form, 'logoUrl', res.data.url);
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 1MB!');
      }
      return isLt2M;
    },
    confirm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          try {
            this.saveLoading = true;
            if (!this.isEdit) {
              await company_add(this.form);
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            } else {
              await company_update(this.form);
              this.$message({
                type: 'success',
                message: '更新成功'
              });
            }
            this.saveLoading = false;
            this.isShow = false;
            this.getCompanies();
          } catch (error) {
            this.saveLoading = false;
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.sign {
  color: #9e9e9e;
}
</style>
